//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: InstallForm.cs
//
// Purpose: Repository/Install Dialog
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     08/18/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
// @EndHeader@
//*********************************************************************

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace H2V2.ConfigUtility
{
	/// <summary>
	/// Summary description for InstallForm.
	/// </summary>
	public class InstallForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckBox cbIntSec;
		private System.Windows.Forms.TextBox tbServer;
		private System.Windows.Forms.TextBox tbDb;
		private System.Windows.Forms.TextBox tbUser;
		private System.Windows.Forms.TextBox tbPwd;
		private System.Windows.Forms.Label lbUser;
		private System.Windows.Forms.Label lbPwd;
		private System.Windows.Forms.Button btInstall;
		private System.Windows.Forms.Button btCancel;
		private System.Windows.Forms.StatusBar statusBar;
		private System.Windows.Forms.Button btFileOpen;
		private System.Windows.Forms.Label lbFile;
		private System.Windows.Forms.TextBox tbFile;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		Exception m_ex;
		private System.Windows.Forms.CheckBox cbWatson;
		int m_nAttempts = 0;

		public InstallForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cbIntSec = new System.Windows.Forms.CheckBox();
			this.tbServer = new System.Windows.Forms.TextBox();
			this.tbDb = new System.Windows.Forms.TextBox();
			this.tbUser = new System.Windows.Forms.TextBox();
			this.tbPwd = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.lbUser = new System.Windows.Forms.Label();
			this.lbPwd = new System.Windows.Forms.Label();
			this.btInstall = new System.Windows.Forms.Button();
			this.btCancel = new System.Windows.Forms.Button();
			this.statusBar = new System.Windows.Forms.StatusBar();
			this.btFileOpen = new System.Windows.Forms.Button();
			this.lbFile = new System.Windows.Forms.Label();
			this.tbFile = new System.Windows.Forms.TextBox();
			this.cbWatson = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// cbIntSec
			// 
			this.cbIntSec.Checked = true;
			this.cbIntSec.CheckState = System.Windows.Forms.CheckState.Checked;
			this.cbIntSec.Location = new System.Drawing.Point(128, 120);
			this.cbIntSec.Name = "cbIntSec";
			this.cbIntSec.Size = new System.Drawing.Size(136, 24);
			this.cbIntSec.TabIndex = 2;
			this.cbIntSec.Text = "Integrated Security";
			this.cbIntSec.CheckedChanged += new System.EventHandler(this.cbIntSec_CheckedChanged);
			// 
			// tbServer
			// 
			this.tbServer.Location = new System.Drawing.Point(128, 40);
			this.tbServer.MaxLength = 256;
			this.tbServer.Name = "tbServer";
			this.tbServer.Size = new System.Drawing.Size(184, 20);
			this.tbServer.TabIndex = 0;
			this.tbServer.Text = "(local)";
			// 
			// tbDb
			// 
			this.tbDb.Location = new System.Drawing.Point(128, 81);
			this.tbDb.MaxLength = 256;
			this.tbDb.Name = "tbDb";
			this.tbDb.Size = new System.Drawing.Size(184, 20);
			this.tbDb.TabIndex = 1;
			this.tbDb.Text = "SQLH2Repository";
			// 
			// tbUser
			// 
			this.tbUser.Enabled = false;
			this.tbUser.Location = new System.Drawing.Point(128, 161);
			this.tbUser.MaxLength = 256;
			this.tbUser.Name = "tbUser";
			this.tbUser.Size = new System.Drawing.Size(184, 20);
			this.tbUser.TabIndex = 3;
			this.tbUser.Text = "";
			// 
			// tbPwd
			// 
			this.tbPwd.Enabled = false;
			this.tbPwd.Location = new System.Drawing.Point(128, 209);
			this.tbPwd.MaxLength = 128;
			this.tbPwd.Name = "tbPwd";
			this.tbPwd.PasswordChar = '*';
			this.tbPwd.Size = new System.Drawing.Size(184, 20);
			this.tbPwd.TabIndex = 4;
			this.tbPwd.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(32, 39);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 23);
			this.label1.TabIndex = 5;
			this.label1.Text = "Server:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 80);
			this.label2.Name = "label2";
			this.label2.TabIndex = 6;
			this.label2.Text = "Database:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lbUser
			// 
			this.lbUser.Enabled = false;
			this.lbUser.Location = new System.Drawing.Point(16, 160);
			this.lbUser.Name = "lbUser";
			this.lbUser.TabIndex = 7;
			this.lbUser.Text = "User:";
			this.lbUser.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lbPwd
			// 
			this.lbPwd.Enabled = false;
			this.lbPwd.Location = new System.Drawing.Point(16, 208);
			this.lbPwd.Name = "lbPwd";
			this.lbPwd.TabIndex = 8;
			this.lbPwd.Text = "Password:";
			this.lbPwd.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// btInstall
			// 
			this.btInstall.Location = new System.Drawing.Point(128, 296);
			this.btInstall.Name = "btInstall";
			this.btInstall.Size = new System.Drawing.Size(112, 23);
			this.btInstall.TabIndex = 10;
			this.btInstall.Text = "Install";
			this.btInstall.Click += new System.EventHandler(this.btInstall_Click);
			// 
			// btCancel
			// 
			this.btCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btCancel.Location = new System.Drawing.Point(280, 296);
			this.btCancel.Name = "btCancel";
			this.btCancel.Size = new System.Drawing.Size(104, 23);
			this.btCancel.TabIndex = 11;
			this.btCancel.Text = "Cancel";
			this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
			// 
			// statusBar
			// 
			this.statusBar.Location = new System.Drawing.Point(0, 336);
			this.statusBar.Name = "statusBar";
			this.statusBar.Size = new System.Drawing.Size(560, 22);
			this.statusBar.TabIndex = 12;
			// 
			// btFileOpen
			// 
			this.btFileOpen.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.btFileOpen.Location = new System.Drawing.Point(488, 8);
			this.btFileOpen.Name = "btFileOpen";
			this.btFileOpen.Size = new System.Drawing.Size(32, 23);
			this.btFileOpen.TabIndex = 19;
			this.btFileOpen.Text = "...";
			this.btFileOpen.Click += new System.EventHandler(this.btFileOpen_Click);
			// 
			// lbFile
			// 
			this.lbFile.ImageAlign = System.Drawing.ContentAlignment.TopRight;
			this.lbFile.Location = new System.Drawing.Point(24, 8);
			this.lbFile.Name = "lbFile";
			this.lbFile.TabIndex = 18;
			this.lbFile.Text = "Script File:";
			this.lbFile.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tbFile
			// 
			this.tbFile.Location = new System.Drawing.Point(128, 8);
			this.tbFile.Name = "tbFile";
			this.tbFile.Size = new System.Drawing.Size(360, 20);
			this.tbFile.TabIndex = 17;
			this.tbFile.Text = "H2DBsetup.sql";
			// 
			// cbWatson
			// 
			this.cbWatson.Location = new System.Drawing.Point(128, 248);
			this.cbWatson.Name = "cbWatson";
			this.cbWatson.Size = new System.Drawing.Size(304, 24);
			this.cbWatson.TabIndex = 20;
			this.cbWatson.Text = "Allow sending data to Microsoft from this Repository";
			// 
			// InstallForm
			// 
			this.AcceptButton = this.btInstall;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btCancel;
			this.ClientSize = new System.Drawing.Size(560, 358);
			this.Controls.Add(this.cbWatson);
			this.Controls.Add(this.btFileOpen);
			this.Controls.Add(this.lbFile);
			this.Controls.Add(this.tbFile);
			this.Controls.Add(this.tbPwd);
			this.Controls.Add(this.tbUser);
			this.Controls.Add(this.tbDb);
			this.Controls.Add(this.tbServer);
			this.Controls.Add(this.statusBar);
			this.Controls.Add(this.btCancel);
			this.Controls.Add(this.btInstall);
			this.Controls.Add(this.lbPwd);
			this.Controls.Add(this.lbUser);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.cbIntSec);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "InstallForm";
			this.Text = "Repository Installation";
			this.ResumeLayout(false);

		}
		#endregion

		private void cbIntSec_CheckedChanged(object sender, System.EventArgs e)
		{
			lbUser.Enabled = !cbIntSec.Checked;
			tbUser.Enabled = !cbIntSec.Checked;
			lbPwd.Enabled = !cbIntSec.Checked;
			tbPwd.Enabled = !cbIntSec.Checked;
		}

		private void btCancel_Click(object sender, System.EventArgs e)
		{
			this.Close ();
		}

		private void btInstall_Click(object sender, System.EventArgs e)
		{
			InstallDbWrapper ();
		}

		#region Installation
		public void InstallDbWrapper ()
		{
			bool ret;

			this.btCancel.Enabled = false;
			//this.btCheck.Enabled = false;
			this.btInstall.Enabled = false;
			this.tbDb.Enabled = false;
			this.tbFile.Enabled = false;
			this.tbPwd.Enabled = false;
			this.tbServer.Enabled = false;
			this.tbUser.Enabled = false;
			this.cbIntSec.Enabled = false;

			Cursor.Current = Cursors.WaitCursor;

			ret = InstallDb ();
			
			Cursor.Current = Cursors.Default;

			if (ret)
			{
				DialogResult dr = MessageBox.Show ("Repository Database was successfully installed!", 
					"Repository installation", MessageBoxButtons.OK);

				SetDefaultRep ();
				
				this.DialogResult = DialogResult.OK;
				this.Close ();
			}
			else
			{
				MessageBox.Show (m_ex.Message, "Repository installation", 
					MessageBoxButtons.OK, MessageBoxIcon.Error);

				this.btCancel.Enabled = true;
				this.btInstall.Enabled = true;
				this.tbFile.Enabled = true;
				this.tbServer.Enabled = true;
				this.tbDb.Enabled = true;
				this.cbIntSec.Enabled = true;
				if (!cbIntSec.Checked)
				{
					this.tbUser.Enabled = true;
					this.tbPwd.Enabled = true;
				}
			}
		}

		public bool InstallDb ()
		{
			bool r;
			int qObjects;
			DbInstaller dbi = null;

			r = CheckConnectionArgs ();

			try
			{
				if (cbIntSec.Checked)
				{
					dbi = new DbInstaller (tbServer.Text, tbDb.Text);
				}
				else
				{
					dbi = new DbInstaller (tbServer.Text, tbDb.Text, tbUser.Text, tbPwd.Text);
				}

				dbi.InitFile (tbFile.Text);
			}
			catch (System.IO.FileNotFoundException fnf)
			{
				m_ex = fnf;
				r = false;
			}
			catch (System.Data.SqlClient.SqlException sex)
			{
				m_ex = sex;
				r = false;
			}
			catch (Exception ex)
			{
				m_ex = ex;
				r = false;
			}

			if (r)
			{
				dbi.OnStatusChangeHandler += new DbInstaller.StatusChangeEventHandler(OnStatusChange);

				try
				{
					qObjects = dbi.CheckExistence ();

					if (qObjects > 0) 
					{
						MessageBox.Show ("Database "+tbDb.Text+" already exists and has "+qObjects+" user objects", "Checing Database existense", MessageBoxButtons.OK);
					}
					else 
					{
						r = dbi.InstallDb ();
						if (r)
						{
							int w = cbWatson.Checked ? 1 : 0;
							dbi.SetWatson (w);
						}
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show (ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}

			if (dbi != null) 
			{
				dbi.Close (); 
			}

			return r;
		}

		bool CheckConnectionArgs ()
		{
			string msg = "";
			bool ret = true;

			if (tbServer.Text == "")
			{
				msg = "You must specify Server name!";
				ret = false;
			}
			else if (tbDb.Text == "") 
			{
				msg = "You must specify Database name!";
				ret = false;
			}
			else if (!cbIntSec.Checked && tbUser.Text == "")
			{
				msg = "You must specify User name or use Integrated Security option!";
				ret = false;
			}
			
			if (!ret)
			{
				MessageBox.Show (msg, "Repository Installation", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return ret;
		}

		void OnStatusChange (object source, StatusChangeEventArgs args)
		{
			if (args.m_IsException)
			{
				m_ex = args.m_exception;
				statusBar.Text = m_ex.Message;
			}
			else
			{
				statusBar.Text = args.m_text;
			}

			statusBar.Update ();
		}
		
		#endregion

		private void btFileOpen_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog ();

			ofd.InitialDirectory = ".";
			ofd.FileName = tbFile.Text;

			if (ofd.ShowDialog () == DialogResult.OK)
			{
				tbFile.Text = ofd.FileName;
				if (m_nAttempts > 0) InstallDbWrapper ();
			}
		}

		void SetDefaultRep ()
		{
			MainForm.stRep.Server = tbServer.Text;
			MainForm.stRep.Database = tbDb.Text;
			MainForm.stRep.bIntSec = cbIntSec.Checked;
			MainForm.stRep.User = tbUser.Text;
		}
	}
}
